﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NasuTek.Shion.Agent;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace NasuTek.Shion.Extencibility
{
    public class Executer
    {
        public static void Execute(ExtencibilityManager em, string command) { Execute(em, command, em.activeServer); }
        public static void Execute(ExtencibilityManager em, string command, string server)
        {
            string[] textmodesplit = command.Split(' ');

            switch (command.Substring(1).Split(' ')[0])
            {
                case "me":
                    em.Clients[server].SendMessage(SendType.Action, em.activeChannel, command.Substring(4));
                    break;
                case "say":
                    em.Clients[server].SendMessage(SendType.Message, em.activeChannel, command.Substring(textmodesplit[0].Length + 1));
                    break;
                case "msg":
                    em.Clients[server].SendMessage(SendType.Message, textmodesplit[1], command.Substring(textmodesplit[0].Length + textmodesplit[1].Length + 2));
                    break;
                case "join":
                    em.Clients[server].RfcJoin(textmodesplit[1]);
                    break;
                case "raw":
                    em.Clients[server].WriteLine(command.Substring(textmodesplit[0].Length + 1));
                    break;
                case "cycle":
                    em.Clients[server].RfcPart(em.activeChannel, "Boing!");
                    em.Clients[server].RfcJoin(em.activeChannel);
                    break;
                case "timer":
                    new ShionTimer(em, command.Substring(textmodesplit[0].Length + textmodesplit[1].Length + textmodesplit[2].Length + 3), Convert.ToInt32(textmodesplit[1]), Convert.ToInt32(textmodesplit[2]));
                    break;
                case "load":
                    try
                    {
                        em.RegisterNewPlugin(command.Substring(textmodesplit[0].Length + 1));
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message != "could not invoke method")
                        {
                            MessageBox.Show(ex.ToString());
                        }
                    }
                    break;
                case "exe":
                    try
                    {
                        Dictionary<string, string> D = new Dictionary<string, string>();
                        D.Add("em.activeServer", em.activeServer);
                        D.Add("em.activeChannel", em.activeChannel);
                        em.Aliases[textmodesplit[1]].Execute(textmodesplit, D, em);
                    }
                    catch (Exception ex) { MessageBox.Show(ex.ToString()); }
                    break;
                default:
                    em.Clients[server].WriteLine(command.Remove(0, 1));
                    break;
            }
        }
    }

    public class ShionTimer : System.Timers.Timer
    {
        int _Times;
        int i = 0;
        string _Command;
        string _Server;
        ExtencibilityManager em;

        public ShionTimer(ExtencibilityManager e, string command, int times, int milliseconds) : this(e, command, times, milliseconds, e.activeServer) { }
        public ShionTimer(ExtencibilityManager e, string command, int times, int milliseconds, string server)
        {
            em = e;
            _Server = server;
            this._Command = command;
            this._Times = times;
            this.Interval = milliseconds;
            this.Elapsed += new System.Timers.ElapsedEventHandler(ShionTimer_Elapsed);
            this.Start();
        }

        public void ShionTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if ((i != Times) || (Times == 0))
            {
                Executer.Execute(em, Command, _Server);
                i++;
            }
            else
            {
                this.Stop();
                this.Dispose();
            }
        }

        public string Command
        {
            get { return _Command; }
            set { _Command = value; }
        }

        public int Times
        {
            get { return _Times; }
            set { _Times = value; }
        }

    }

    public class ShionMenuItem : ToolStripMenuItem
    {
        string _Command;
        ExtencibilityManager em;

        public ShionMenuItem(string MenuEntry, ToolStripMenuItem whereToAdd)
        {
            this.Text = MenuEntry;
            whereToAdd.DropDownItems.Add(this);
        }
        public ShionMenuItem(ExtencibilityManager e, string MenuEntry, string command, ToolStripMenuItem whereToAdd)
        {
            em = e;
            this._Command = command;
            this.Text = MenuEntry;
            this.Click += new EventHandler(ShionMenuItem_Click);
            whereToAdd.DropDownItems.Add(this);
        }

        void ShionMenuItem_Click(object sender, EventArgs e)
        {
            Regex regex = new Regex(@"([\$]{2})\?\=\((?<name>.*?)\)");
            MatchCollection mc = regex.Matches(_Command);
            foreach (Match i in mc)
            {
                GetDialog get = new GetDialog(i.Groups["name"].ToString());
                if (get.ShowDialog() == DialogResult.OK)
                {
                    _Command = _Command.Replace("$$?=(" + i.Groups["name"].ToString() + ")", get.Answer);
                }
                else
                {
                    _Command = "";
                    break;
                }
            }
            if (_Command != "")
            {
                Executer.Execute(em, _Command, em.activeServer);
            }
        }
    }
}